package kordegi.forumsystem.service.Impl;

import io.jsonwebtoken.lang.Assert;
import kordegi.forumsystem.entity.User;
import kordegi.forumsystem.mapper.UserMapper;
import kordegi.forumsystem.service.UserService;
import kordegi.forumsystem.shiro.UserProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;
import java.util.Set;

/**
 * @author kordegi
 * @date 2022.05.01 15:30
 * @Description
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public User getById(Long id) {
        return userMapper.selectUser(id);
    }

    @Override
    public User login(String username) {
        User userMap = userMapper.login(username);
        Assert.notNull(userMap, "登陆失败，用户为空");

        return userMap;
    }

    @Override
    public boolean signIn(String username, String password, String email) {
        Random random = new Random(Long.MAX_VALUE);
        User user = new User();
        user.setId(random.nextLong());
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        int i = userMapper.register(user.getId(), user.getUsername(), user.getPassword(),user.getEmail());
        if (i > 0){
            return true; //注册成功！
        } else {
            return false; //注册失败！
        }
    }

    @Override
    public List<User> queryAll() {
        return userMapper.queryAll();
    }
}
